<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>C++ Web Framework: /home/herik/CPPWebFramework/CPPWebFramework/cwf/sqldatabasestorage.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">C++ Web Framework
   &#160;<span id="projectnumber">3.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_b2448bd9c6b97c57da490a507d179d6c.html">CPPWebFramework</a></li><li class="navelem"><a class="el" href="dir_3bacee99d34edff13677baafd8c3a47f.html">cwf</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">sqldatabasestorage.h</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> Distributed under MIT license, or public domain if desired and</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> recognized in your jurisdiction.</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> See file LICENSE for detail.</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment">*/</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#ifndef SQLDATABASESTORAGE_H</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor">#define SQLDATABASESTORAGE_H</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#include &lt;QUuid&gt;</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="preprocessor">#include &lt;QDebug&gt;</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="preprocessor">#include &lt;QSqlError&gt;</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="preprocessor">#include &lt;QSqlDatabase&gt;</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="preprocessor">#include &lt;QThreadStorage&gt;</span></div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="preprocessor">#include &quot;cppwebframework_global.h&quot;</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;CWF_BEGIN_NAMESPACE</div><div class="line"><a name="l00022"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html">   22</a></span>&#160;<span class="keyword">class </span>CPPWEBFRAMEWORKSHARED_EXPORT <a class="code" href="class_sql_database_storage.html">SqlDatabaseStorage</a></div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;{    </div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    <span class="keyword">class </span>Database</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;    {</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;        <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="class_sql_database_storage.html">SqlDatabaseStorage</a>;</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;        QSqlDatabase *db = <span class="keyword">nullptr</span>;</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;    <span class="keyword">public</span>:</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        Database() = <span class="keywordflow">default</span>;</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;        Database(Database &amp;other)</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;        {</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;            db = other.db;</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;            other.db = <span class="keyword">nullptr</span>;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;        }</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;        ~Database()</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;        {</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;            <span class="keywordflow">if</span>(db)</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;            {</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;                <span class="keyword">const</span> QString conName(db-&gt;connectionName());</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;                db-&gt;close();</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;                <span class="keyword">delete</span> db;</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;                QSqlDatabase::removeDatabase(conName);</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;            }</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;        }</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    };</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;    QString type;</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;    QString hostName;</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    QString databaseName;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;    QString userName;</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;    QString password;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    <span class="keywordtype">int</span> port;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    QThreadStorage&lt;Database&gt; pool;</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;<span class="keyword">public</span>:</div><div class="line"><a name="l00063"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#a691465c618b8050f5c67543f57fc4d15">   63</a></span>&#160;    <a class="code" href="class_sql_database_storage.html#a691465c618b8050f5c67543f57fc4d15">SqlDatabaseStorage</a>(<span class="keyword">const</span> QString &amp;type = <span class="stringliteral">&quot;&quot;</span>, <span class="keyword">const</span> QString &amp;hostName = <span class="stringliteral">&quot;&quot;</span>,</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;                       <span class="keyword">const</span> QString &amp;databaseName = <span class="stringliteral">&quot;&quot;</span>, <span class="keyword">const</span> QString &amp;userName = <span class="stringliteral">&quot;&quot;</span>,</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;                       <span class="keyword">const</span> QString &amp;password = <span class="stringliteral">&quot;&quot;</span>, <span class="keywordtype">int</span> port = 0) : type(type), hostName(hostName),</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;                                                                    databaseName(databaseName), userName(userName),</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;                                                                    password(password), port(port)</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;    {</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    }</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;</div><div class="line"><a name="l00074"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#af63f84ae9263c490325f10bbb352c983">   74</a></span>&#160;    <span class="keyword">inline</span> QString <a class="code" href="class_sql_database_storage.html#af63f84ae9263c490325f10bbb352c983">getType</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> type; }</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;</div><div class="line"><a name="l00079"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#a1f87803a1217305b4bc05fd13bf36ba6">   79</a></span>&#160;    <span class="keyword">inline</span> QString <a class="code" href="class_sql_database_storage.html#a1f87803a1217305b4bc05fd13bf36ba6">getPassword</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> password; }</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;</div><div class="line"><a name="l00084"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#a23a969c1fd094a15b1787add541e9bb5">   84</a></span>&#160;    <span class="keyword">inline</span> QString <a class="code" href="class_sql_database_storage.html#a23a969c1fd094a15b1787add541e9bb5">getHostName</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> hostName; }</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;</div><div class="line"><a name="l00089"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#a69dd2e9296bc727fa396316d1746dee8">   89</a></span>&#160;    <span class="keyword">inline</span> QString <a class="code" href="class_sql_database_storage.html#a69dd2e9296bc727fa396316d1746dee8">getDatabaseName</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> databaseName; }</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;</div><div class="line"><a name="l00094"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#afc4b98c1eb06e868ce0b317aa381a899">   94</a></span>&#160;    <span class="keyword">inline</span> QString <a class="code" href="class_sql_database_storage.html#afc4b98c1eb06e868ce0b317aa381a899">getUserName</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> userName; }</div><div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;</div><div class="line"><a name="l00099"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#acf0079bd639d2e4ba91c9b007a6af263">   99</a></span>&#160;    <span class="keyword">inline</span> <span class="keywordtype">int</span> <a class="code" href="class_sql_database_storage.html#acf0079bd639d2e4ba91c9b007a6af263">getPort</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> port; }</div><div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;</div><div class="line"><a name="l00104"></a><span class="lineno"><a class="line" href="class_sql_database_storage.html#a4184a8138d1f65bc644825f587436aea">  104</a></span>&#160;    QSqlDatabase &amp;<a class="code" href="class_sql_database_storage.html#a4184a8138d1f65bc644825f587436aea">getDatabase</a>()</div><div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;    {</div><div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;        <span class="keywordflow">if</span>(!pool.hasLocalData())</div><div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;        {</div><div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;            Database database;</div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;            database.db = <span class="keyword">new</span> QSqlDatabase(QSqlDatabase::addDatabase(type, QUuid::createUuid().toString()));</div><div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;            database.db-&gt;setHostName(hostName);</div><div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;            database.db-&gt;setDatabaseName(databaseName);</div><div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;            database.db-&gt;setPort(port);</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;            database.db-&gt;setUserName(userName);</div><div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;            database.db-&gt;setPassword(password);</div><div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;            <span class="keywordflow">if</span>(!database.db-&gt;open())</div><div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;                qDebug() &lt;&lt; database.db-&gt;lastError().text();</div><div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;            pool.setLocalData(database);</div><div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;        }</div><div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;        <span class="keywordflow">return</span> *pool.localData().db;</div><div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;    }</div><div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;};</div><div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;</div><div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;CWF_END_NAMESPACE</div><div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;</div><div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;<span class="preprocessor">#endif // SQLDATABASESTORAGE_H</span></div><div class="ttc" id="class_sql_database_storage_html_a23a969c1fd094a15b1787add541e9bb5"><div class="ttname"><a href="class_sql_database_storage.html#a23a969c1fd094a15b1787add541e9bb5">SqlDatabaseStorage::getHostName</a></div><div class="ttdeci">QString getHostName() const</div><div class="ttdoc">Returns the host name. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:84</div></div>
<div class="ttc" id="class_sql_database_storage_html_a4184a8138d1f65bc644825f587436aea"><div class="ttname"><a href="class_sql_database_storage.html#a4184a8138d1f65bc644825f587436aea">SqlDatabaseStorage::getDatabase</a></div><div class="ttdeci">QSqlDatabase &amp; getDatabase()</div><div class="ttdoc">Returns the existing connection to the current thread&amp;#39;s database. If there is no open connection to t...</div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:104</div></div>
<div class="ttc" id="class_sql_database_storage_html_a69dd2e9296bc727fa396316d1746dee8"><div class="ttname"><a href="class_sql_database_storage.html#a69dd2e9296bc727fa396316d1746dee8">SqlDatabaseStorage::getDatabaseName</a></div><div class="ttdeci">QString getDatabaseName() const</div><div class="ttdoc">Returns the database&amp;#39;s name. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:89</div></div>
<div class="ttc" id="class_sql_database_storage_html_afc4b98c1eb06e868ce0b317aa381a899"><div class="ttname"><a href="class_sql_database_storage.html#afc4b98c1eb06e868ce0b317aa381a899">SqlDatabaseStorage::getUserName</a></div><div class="ttdeci">QString getUserName() const</div><div class="ttdoc">Returns the user&amp;#39;s name. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:94</div></div>
<div class="ttc" id="class_sql_database_storage_html_acf0079bd639d2e4ba91c9b007a6af263"><div class="ttname"><a href="class_sql_database_storage.html#acf0079bd639d2e4ba91c9b007a6af263">SqlDatabaseStorage::getPort</a></div><div class="ttdeci">int getPort() const</div><div class="ttdoc">Returns the port. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:99</div></div>
<div class="ttc" id="class_sql_database_storage_html"><div class="ttname"><a href="class_sql_database_storage.html">SqlDatabaseStorage</a></div><div class="ttdoc">The SqlDatabaseStorage class allows you to reuse connections made to the database through the QSqlDat...</div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:22</div></div>
<div class="ttc" id="class_sql_database_storage_html_a691465c618b8050f5c67543f57fc4d15"><div class="ttname"><a href="class_sql_database_storage.html#a691465c618b8050f5c67543f57fc4d15">SqlDatabaseStorage::SqlDatabaseStorage</a></div><div class="ttdeci">SqlDatabaseStorage(const QString &amp;type=&quot;&quot;, const QString &amp;hostName=&quot;&quot;, const QString &amp;databaseName=&quot;&quot;, const QString &amp;userName=&quot;&quot;, const QString &amp;password=&quot;&quot;, int port=0)</div><div class="ttdoc">This constructor receives informations to create a connection to the database. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:63</div></div>
<div class="ttc" id="class_sql_database_storage_html_af63f84ae9263c490325f10bbb352c983"><div class="ttname"><a href="class_sql_database_storage.html#af63f84ae9263c490325f10bbb352c983">SqlDatabaseStorage::getType</a></div><div class="ttdeci">QString getType() const</div><div class="ttdoc">Returns the type. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:74</div></div>
<div class="ttc" id="class_sql_database_storage_html_a1f87803a1217305b4bc05fd13bf36ba6"><div class="ttname"><a href="class_sql_database_storage.html#a1f87803a1217305b4bc05fd13bf36ba6">SqlDatabaseStorage::getPassword</a></div><div class="ttdeci">QString getPassword() const</div><div class="ttdoc">Returns the password. </div><div class="ttdef"><b>Definition:</b> sqldatabasestorage.h:79</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Mon May 28 2018 11:09:08 for C++ Web Framework by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
